package cn.pli.bike.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;

import com.bigkoo.alertview.OnItemClickListener;
import com.framemodule.base.BaseActivityPresenter;
import com.framemodule.base.netstatus.NetUtils;
import com.framemodule.utils.CommonUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.androidcommon.adapter.BGAOnItemChildClickListener;
import cn.pli.bike.R;
import cn.pli.bike.bean.EventAction;
import cn.pli.bike.bean.HttpResult;
import cn.pli.bike.bean.ProblemBean;
import cn.pli.bike.model.UserModel;
import cn.pli.bike.model.http.ApiException;
import cn.pli.bike.model.subscribers.ProgramSubscriber;
import cn.pli.bike.utils.AppUtils;

/**
 * 客服中心
 */

public class CallCenterActivity extends BaseActivityPresenter<CallCenterDelegate> implements View.OnClickListener {
    public static final int REPORT_FAULT = 0;           //上报故障
    public static final int REPORT_ILLDAGAL = 1;        //举报违章
    public static final int BACK_DEPOSIT = 2;           //押金退还
    public static final int RECHARGE = 3;               //充值车费
    private int type;                                   //类型
    private String title;


    @Override
    protected void initModel() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getData();
        viewDelegate.getCallCenterMainAdapter().setOnItemChildClickListener(new BGAOnItemChildClickListener() {
            @Override
            public void onItemChildClick(ViewGroup parent, View childView, int position) {
                Bundle bundle = new Bundle();
                switch (childView.getId()) {
                    //上报故障
                    case R.id.tv_report_fault:
                        title = getString(R.string.report_fault);
                        type = REPORT_FAULT;
                        bundle.putInt("type", type);
                        bundle.putString("title", title);
                        readyGo(ProblemDetailActivity.class, bundle);

                        break;
                    //举报违停
                    case R.id.tv_report_illegal_parking:
                        title = getString(R.string.report_illegal_parking);
                        type = REPORT_ILLDAGAL;
                        bundle.putInt("type", type);
                        bundle.putString("title", title);
                        readyGo(ProblemDetailActivity.class, bundle);
                        break;
                    //退还押金
                    case R.id.tv_back_deposit:
                        backDeposit();

                        break;
                    //充值缴费
                    case R.id.tv_recharge:
                        title = getString(R.string.recharge);
                        type = RECHARGE;
                        readyGo(YourWalletActivity.class);
                        break;
                    //联系客服
                    case R.id.tv_connect_service:
                        String phone = AppUtils.getComPhone();
                        if (CommonUtils.isNotEmpty(phone)) {
                            call(phone);
                        }
                        break;


                }
            }
        });

        EventBus.getDefault().register(this);


    }


    private void backDeposit() {
        viewDelegate.showAlert("", "确定要退吗？"/*提示*/, new
                OnItemClickListener() {
                    @Override
                    public void onItemClick(Object o, int position) {
                        if (position == 1) {
                            UserModel.refundMoney(new ProgramSubscriber<HttpResult<String>>() {
                                @Override
                                public void onNext(HttpResult<String> stringHttpResult) {
                                    viewDelegate.toast(stringHttpResult.getData());
                                }

                                @Override
                                public void onErr(ApiException e) {
                                    viewDelegate.toast(e.getMsg());
                                    super.onErr(e);
                                }
                            });

                        }

                    }
                }
        );
    }

    private void call(final String phone) {
        viewDelegate.showAlert1("呼叫:" + phone, "确定", new OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
        }

    }

    /**
     * 获取问题列表
     */
    private void getData() {
        UserModel.commonProblem(new ProgramSubscriber<HttpResult<List<ProblemBean>>>() {
            @Override
            public void onNext(HttpResult<List<ProblemBean>> listHttpResult) {
                List<List<ProblemBean>> data = new ArrayList<>();
                data.add(listHttpResult.getData());
                viewDelegate.getCallCenterMainAdapter().setData(data);
            }
        });
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        viewDelegate.setOnClickListener(this, R.id.iv_title_back);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleWeb(Message message) {
        switch (message.what) {
            case EventAction.ACTION_COMMON_PROBLEM:
                ProblemBean.DetailProblemBean model = (ProblemBean.DetailProblemBean) message.obj;
                Bundle bundle = new Bundle();
                bundle.putString("title", model.getTitle());
                bundle.putString("url", model.getUrl());
                readyGo(WebViewActivity.class, bundle);
                break;

        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
